import { _decorator, assetManager, Component, director, Label, Node, resources, Sprite, SpriteFrame } from 'cc';
import { App } from '../App';
import { EventConst } from '../common/Common';
import UtilTool from '../tools/UtilTool';

const { ccclass, property } = _decorator;

@ccclass('LanguageSprite')
export class LanguageSprite extends Component {
    @property({ type: String, tooltip: '图片名字' })
    spriteName: String = '';
    @property({ type: Boolean, tooltip: '是否有其他状态' })
    isState: Boolean = false;

    upDateSprite() {
        if  (!this.node || !this.node.getComponent(Sprite)) return;
        let url = App.language.getLanguageImgUrl(this.spriteName);

        this.node.active = false
        resources.load(url, SpriteFrame, (err, spriteFrame) => {
            if (err) {
                UtilTool.printLog("*************************动态加载资源失败,请检查资源路径是否正确");
                return
            }
            if  (!this.node || !this.node.getComponent(Sprite)) return;
            this.node.active = true
            this.node.getComponent(Sprite).spriteFrame = spriteFrame;
        })
    }

    start() {
        App.Event.on(EventConst.CHANGE_LANGUAGE, this.upDateSprite, this)
        this.upDateSprite()
    }

    update(deltaTime: number) {

    }
}

